<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="姓名" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="姓名"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证件号码" prop="cardno">
        <el-input
            v-model="queryParams.cardno"
            placeholder="证件号码"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="jzList">
      <el-table-column label="姓名" align="center" prop="name"/>
      <el-table-column label="证件号码" align="center" prop="cardno"/>
      <el-table-column label="联系方式" align="center" prop="phone"/>
      <el-table-column label="区域" align="center" prop="addr"/>
      <el-table-column label="详细地址" align="center" prop="addrDetail"/>
      <el-table-column label="创建时间" align="center" prop="addtime" width="180"/>
      <el-table-column label="操作" width="220" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" @click="handleDetail(scope.row)" v-hasPermi="['by:jz:remove']">个人信息详情</el-button>
          <el-button link type="primary" @click="handleOrderDetail(scope.row)" v-hasPermi="['by:jz:remove']">订单详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 打开家长信息详情 -->
    <el-dialog title="家长信息详情" v-model="openDetailDialog" width="700px" append-to-body>
      <el-form :model="formDetail" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="家长姓名：">{{ formDetail.name }}</el-form-item>
            <el-form-item label="证件号码：">{{ formDetail.cardno}}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式：">{{ formDetail.phone }}</el-form-item>
            <el-form-item label="孩子姓名：">{{ formDetail.stuname }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="详细地址：">{{ formDetail.addr }}/{{ formDetail.addrDetail }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="人脸识别：">
              <div v-if="formDetail.status === '0'">未设置</div>
              <div v-else-if="formDetail.status === '1'">识别成功</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="更新时间：">{{ parseTime(formDetail.updatetime) }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="closeDetailDialog">关 闭</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 打开家长订单列表 -->
    <el-dialog title="订单列表" v-model="openOrderListDialog" width="900px" append-to-body>
      <el-table v-loading="orderListLoading" :data="orderList">
        <el-table-column label="联系人" align="center" prop="lxr"/>
        <el-table-column label="联系方式" align="center" prop="phone"/>
        <el-table-column label="科目" align="center" prop="km"/>
        <el-table-column label="时间" align="center" prop="time"/>
        <el-table-column label="详细地址" align="center" prop="address" :show-overflow-tooltip="true"/>
        <el-table-column label="创建时间" align="center" prop="addtime" width="180"/>
      </el-table>
      <pagination
              v-show="orderListTotal > 0"
              :total="orderListTotal"
              v-model:page="queryOrderParams.pageNum"
              v-model:limit="queryOrderParams.pageSize"
              @pagination="getOrderList"
      />
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="closeOrderListDialog">关 闭</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup>
  import {listJz, getJz} from "@/api/bydll/jz";
  import {listOrder} from "@/api/bydll/order";

  const showSearch = ref(true);
  const jzList = ref([]);
  const orderList = ref([]);
  const open = ref(false);
  const openDetailDialog = ref(false); // 打开家长详情Dialog
  const openOrderListDialog = ref(false); // 打开关联订单Dialog
  const title = ref("");
  const loading = ref(true);
  const orderListLoading = ref(true);
  const total = ref(0);
  const orderListTotal = ref(0);
  const ids = ref([]);
  const jzId = ref(-1);
  const imageUrl = ref('');
  const data = reactive({
    form: {},
    formDetail: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      name: undefined
    },
    queryOrderParams: {
      pageNum: 1,
      pageSize: 10,
      jzId: undefined,
    }
  });
  const {queryParams, queryOrderParams, form, formDetail} = toRefs(data);

  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }

  function getList() {
    loading.value = true;
    listJz(queryParams.value).then(response => {
      jzList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }

  /** 家长信息详情 */
  function handleDetail(row) {
    const jzId = row.id
    getJz(jzId).then(response => {
      formDetail.value = response.data
      openDetailDialog.value = true
    });
  }

  /** 关闭家长详情弹出框 */
  function closeDetailDialog() {
    openDetailDialog.value = false;
    formDetail.value = {};
  }

  /** 用户关联订单列表 */
  function getOrderList() {
    queryOrderParams.value.jzId = jzId.value
    orderListLoading.value = true
    listOrder(queryOrderParams.value).then(response => {
      orderList.value = response.rows;
      orderListTotal.value = response.total;
      orderListLoading.value = false
    });
  }

  function handleOrderDetail(row) {
    jzId.value = row.id
    openOrderListDialog.value = true
    getOrderList()
  }

  /** 关闭订单弹出框 */
  function closeOrderListDialog() {
    openOrderListDialog.value = false
    jzId.value = -1
  }

  getList();

</script>
